package DAO;

  import java.sql.Array;
  import java.sql.Connection;
  import java.sql.Date;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.Vector;

  import oracle.net.TNSAddress.Description;

public class PenDao {

    public boolean creaPen(int capacity,String status, int clinicNum) {

      Conexion conexion = new Conexion();
      boolean creado = false;
      Connection conex;

      try {
        int numberPen;
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        ResultSet rsa = null;
        int mayor = -1;
        ps =
            conex.prepareStatement("select max (numberPen)  from pen");
        rsa = ps.executeQuery();
        if (rsa != null) {
          while (rsa.next()) {
            mayor = rsa.getInt(1);
          }
        } else
          numberPen = 1;


        ps =
            conex.prepareStatement("INSERT INTO PEN(NUMBERPEN,CAPACITY ,STATUS,CLINICNUMBER) VALUES (?,?,?,?)");
        numberPen = mayor + 1;
        ps.setInt(1,  numberPen);
        ps.setInt(2, capacity);
        ps.setString(3, status);
        ps.setInt(4, clinicNum);

        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que inserto fueron" +
                           numeroFilas);
        creado = true;
        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());

      }
      return creado;
    }


    public boolean modificaPenCapacity(int numpen, int cap) {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
System.out.println("Estoy en modcap" + numpen);
        ps =
            conex.prepareStatement("UPDATE pen set capacity = ?  where numberPen = ?");

        ps.setInt(1, cap);

        ps.setInt(2, numpen);

        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que inserto fueron" +
                           numeroFilas);

        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }

    public boolean modificaPenStatus(int numpen, String status) {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;

        System.out.println("Estoy en status" + numpen);
        ps =
            conex.prepareStatement("UPDATE pen set status = ?  where numberPen = ?");

        ps.setString(1, status);

        ps.setInt(2, numpen);

        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que inserto fueron" +
                           numeroFilas);

        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }

    public boolean modificaPenClinic(int numPen, int clinic) {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;


  System.out.println("Estamos en DAO " + numPen);
        ps =
            conex.prepareStatement("UPDATE pen set clinicnumber = ?  where numberPen = ?");

        ps.setInt(1, clinic);

        ps.setInt(2, numPen);

        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que inserto fueron" +
                           numeroFilas);

        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }

    public boolean eliminarPen(int numPen) {

      Conexion conexion = new Conexion();
      boolean eliminado = false;
      Connection conex;

      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        ps = conex.prepareStatement("DELETE FROM pen where numberpen = ?");
        ps.setInt(1, numPen);

        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que borro fueron" + numeroFilas);
        eliminado = true;
        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());

      }
      return eliminado;
    }

    public Object[][] DatosPenTabla() {
      Conexion conexion = new Conexion();
      Connection conex;
      Object[][] datos = new Object[100][100];
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        ps = conex.prepareStatement("select * FROM pen ");     
        int i = 1;
        int j = 0;
        rs = ps.executeQuery();
        System.out.println(rs + " es el resultado de la consulta !");

        while (rs.next()) {
          datos[0][0]=" NUMBER PEN";
          datos[0][1]="CAPACITY";
          datos[0][2]="STATUS";
          datos[0][3]="CLINIC NUMBER";
          datos[i][j] = rs.getInt(1);
          System.out.print(datos[i][j] + " " + "Esta es dao");
          j++;
          datos[i][j] = rs.getInt(2);
          System.out.print(datos[i][j] + " ");
          j++;
          datos[i][j] = rs.getString(3);
          System.out.print(datos[i][j] + " ");
          j++;
          datos[i][j] = rs.getInt(4);
          System.out.print(datos[i][j] + " ");
          j++;
          System.out.println("FIN!! ");
          i++;
          j=0;
        }
        
        rs.close();
        ps.close();
        conex.close();       
        /*for (int f = 1; f < 9; f++) {
          for (int g = 0; g < 9; g++) {
            System.out.println("datos");
            System.out.print(datos[f][g]);
          }
        } */  
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
      }
      return datos;
    }

  public PenDao() {
  }

}
